<?php
use PMA\libraries\DatabaseInterface;
use PMA\libraries\Table;
use PMA\libraries\Util;

$js_msg = '';
$this_params = null;
if (isset($one_key['constraint'])) {
    $drop_fk_query = 'ALTER TABLE ' . PMA\libraries\Util::backquote($GLOBALS['table'])
        . ' DROP FOREIGN KEY '
        . PMA\libraries\Util::backquote($one_key['constraint']) . ';';
    $this_params = $GLOBALS['url_params'];
    $this_params['goto'] = 'tbl_relation.php';
    $this_params['back'] = 'tbl_relation.php';
    $this_params['sql_query'] = $drop_fk_query;
    $this_params['message_to_show'] = sprintf(
        __('Foreign key constraint %s has been dropped'),
        $one_key['constraint']
    );
    $js_msg = PMA_jsFormat(
        'ALTER TABLE ' . $GLOBALS['table']
        . ' DROP FOREIGN KEY '
        . $one_key['constraint'] . ';'
    );
}

// For ON DELETE and ON UPDATE, the default action
// is RESTRICT as per MySQL doc; however, a SHOW CREATE TABLE
// won't display the clause if it's set as RESTRICT.
$on_delete = isset($one_key['on_delete'])
    ? $one_key['on_delete'] : 'RESTRICT';
$on_update = isset($one_key['on_update'])
    ? $one_key['on_update'] : 'RESTRICT';

$column_array = array();
$column_array[''] = '';
foreach ($columns as $column) {
    if (strtoupper($tbl_storage_engine) == 'INNODB'
        || ! empty($column['Key'])
    ) {
        $column_array[$column['Field']] = $column['Field'];
    }
}

$foreign_table = false;
// foreign database dropdown
$foreign_db = (isset($one_key['ref_db_name'])) ? $one_key['ref_db_name'] : $db;

$tables = array();
if ($foreign_db) {
    $foreign_table = isset($one_key['ref_table_name'])
        ? $one_key['ref_table_name'] : '';

    $tables_rs = $GLOBALS['dbi']->query(
        'SHOW TABLE STATUS FROM ' . Util::backquote($foreign_db),
        null,
        DatabaseInterface::QUERY_STORE
    );
    while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
        if (isset($row[1])
            && mb_strtoupper($row[1]) == $tbl_storage_engine
        ) {
            $tables[] = $row[0];
        }
    }
}
?>
<tr class="<?= ($odd_row ? 'odd' : 'even'); ?>">
    <!-- Drop key anchor -->
    <td>
        <?php if (isset($one_key['constraint'])): ?>
            <input type="hidden"
                   class="drop_foreign_key_msg"
                   value="<?= $js_msg; ?>" />
            <a class="drop_foreign_key_anchor ajax"
               href="sql.php<?= PMA_URL_getCommon($this_params); ?>" >
                <?= PMA\libraries\Util::getIcon('b_drop.png', __('Drop')); ?>
            </a>
        <?php endif; ?>
    </td>
    <td>
        <span class="formelement clearfloat">
            <input type="text" name="constraint_name[<?= $i; ?>]"
                   value="<?= (isset($one_key['constraint'])) ?
                       htmlspecialchars($one_key['constraint'])
                       : ''
                       ?>"
                   placeholder="<?= __('Constraint name'); ?>"
                   maxlength="64" />
        </span>
        <div class="floatleft">
            <span class="formelement">
                <?= PMA\libraries\Template::get('table/relation/dropdown_generate')->render(
                    array(
                        'dropdown_question' => 'ON DELETE',
                        'select_name' => 'on_delete[' . $i . ']',
                        'choices' => $options_array,
                        'selected_value' => $on_delete
                    )
                ); ?>
            </span>
            <span class="formelement">
                <?= PMA\libraries\Template::get('table/relation/dropdown_generate')->render(
                    array(
                        'dropdown_question' => 'ON UPDATE',
                        'select_name' => 'on_update[' . $i . ']',
                        'choices' => $options_array,
                        'selected_value' => $on_update
                    )
                ); ?>
            </span>
        </div>
    </td>
    <td>
        <?php if (isset($one_key['index_list'])): ?>
            <?php foreach ($one_key['index_list'] as $key => $column): ?>
                <span class="formelement clearfloat">
                    <?= PMA\libraries\Template::get('table/relation/dropdown_generate')->render(
                        array(
                            'dropdown_question' => '',
                            'select_name' => 'foreign_key_fields_name[' . $i . '][]',
                            'choices' => $column_array,
                            'selected_value' => $column
                        )
                    ); ?>
                </span>
            <?php endforeach; ?>
        <?php else: ?>
            <span class="formelement clearfloat">
                <?= PMA\libraries\Template::get('table/relation/dropdown_generate')->render(
                    array(
                        'dropdown_question' => '',
                        'select_name' => 'foreign_key_fields_name[' . $i . '][]',
                        'choices' => $column_array,
                        'selected_value' => ''
                    )
                ); ?>
            </span>
        <?php endif; ?>
        <a class="formelement clearfloat add_foreign_key_field"
           href=""
           data-index="<?= $i; ?>">
            <?= __('+ Add column'); ?>
        </a>
    </td>
    <td>
        <span class="formelement clearfloat">
            <?= PMA\libraries\Template::get('table/relation/relational_dropdown')->render(
                array(
                    'name' => 'destination_foreign_db[' . $i . ']',
                    'title' => __('Database'),
                    'values' => $GLOBALS['dblist']->databases,
                    'foreign' => $foreign_db
                )
            ); ?>
    </td>
    <td>
        <span class="formelement clearfloat">
            <?= PMA\libraries\Template::get('table/relation/relational_dropdown')->render(
                array(
                    'name' => 'destination_foreign_table[' . $i . ']',
                    'title' => __('Table'),
                    'values' => $tables,
                    'foreign' => $foreign_table
                )
            ); ?>
        </span>
    </td>
    <td>
        <?php if ($foreign_db && $foreign_table): ?>
            <?php foreach ($one_key['ref_index_list'] as $foreign_column): ?>
                <?php
                $table_obj = new Table($foreign_table, $foreign_db);
                $columns = $table_obj->getUniqueColumns(false, false);
                ?>
                <span class="formelement clearfloat">
                    <?= PMA\libraries\Template::get('table/relation/relational_dropdown')->render(
                        array(
                            'name' => 'destination_foreign_column[' . $i . '][]',
                            'title' => __('Column'),
                            'values' => $columns,
                            'foreign' => $foreign_column
                        )
                    ); ?>
                </span>
            <?php endforeach; ?>
        <?php else: ?>
            <span class="formelement clearfloat">
                <?= PMA\libraries\Template::get('table/relation/relational_dropdown')->render(
                    array(
                        'name' => 'destination_foreign_column[' . $i . '][]',
                        'title' => __('Column'),
                        'values' => array(),
                        'foreign' => ''
                    )
                ); ?>
            </span>
        <?php endif; ?>
    </td>
</tr>